﻿using Mall.Infrastructure.Encrypt;
using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sx.Mall.Domain
{
   public partial class MallManager
    {
        /// <summary>
        /// 系统人员添加
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
       public static CommonResult Add(IRepository<MallManager> repository, MallManager entity)
        {
            CommonResult result = new CommonResult();
            repository.Add(entity);
            result.IsResult = true;
            result.Message = "添加成功";
            return result;
        }


       /// <summary>
       /// 修改系统人员
       /// </summary>
       /// <param name="dto"></param>
       /// <returns></returns>
       public static CommonResult Update(IRepository<MallManager> repository, MallManager entity)
       {
           CommonResult result = new CommonResult();
           var item = repository.Where(m => m.UserID == entity.UserID).FirstOrDefault();

           if (item != null && item.UserID != entity.UserID)
           {
               result.IsResult = false;
               result.Message = "名称已经存在";
           }
           else
           {
               item = repository.Where(m => m.UserID == entity.UserID).First();
               item.UserName = entity.UserName;
               item.pwd = entity.pwd;
               item.Status = entity.Status;
               item.RealName = entity.RealName;
               result.IsResult = true;
               result.Message = "更新成功";
           }
           return result;
       }


       /// <summary>
       /// 批量删除系统人员
       /// </summary>
       /// <param name="repository"></param>
       /// <param name="ids"></param>
       /// <returns></returns>
       public static CommonResult DeleteUser(IRepository<MallManager> repository, List<int> ids) 
       {
           CommonResult result = new CommonResult();
           foreach (var id in ids)
           {
               repository.Remove(repository.Where(m=>m.UserID==id).First());
           }
           result.IsResult = true;
           result.Message = "删除成功";
           return result;
       }

       /// <summary>
       /// 系统人员登录
       /// </summary>
       /// <param name="dto"></param>
       /// <returns></returns>
       public static CommonResult<MallManager> Login(IRepository<MallManager> repository, string accout, string password)
       {
           CommonResult<MallManager> result = new CommonResult<MallManager>();
           password = Md5Expand.GetMd5(password);
           var item = repository.Where(m => m.UserName == accout).FirstOrDefault();
           if (item == null)
           {
               result.IsResult = false;
               result.Message = "账号或密码错误";
           }
           else
           {
               if (item.Islocked)
               {
                   result.IsResult = false;
                   result.Message = "密码错误次数过多，已被锁定，请联系管理员解锁";
               }
               else if (item.pwd != password)
               {
                   result.IsResult = false;
                   item.ErrorCount++;
                   if (item.ErrorCount == 10)
                   {
                       item.Islocked = true;
                       item.ErrorCount = 0;
                   }
                   result.Message ="密码输入错误"+ item.ErrorCount+"次";
               }
               else
               {
                   result.IsResult = true;
                   result.Message = "登录成功";
                   result.Result = item;
                   item.ErrorCount = 0;
               }
           }
           return result;
       }
    }
}
